package com.adnaan.backend.controller;

import com.adnaan.backend.common.utils.PageResult;
import com.adnaan.backend.common.utils.PageUtils;
import com.adnaan.backend.common.utils.R;
import com.adnaan.backend.entity.SysRole;
import com.adnaan.backend.service.SysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author: adnaan
 * create: 2024-06-15 22:03
 * Description:
 */
@Tag(name = "角色接口", description = "角色相关功能")
@RestController
@RequestMapping("/api/sys_role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    @Operation(summary = "获取角色列表")
    @GetMapping("/list")
    public R list(@RequestParam Integer pageNum,
                  @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> list = sysRoleService.list();
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return R.success(pageResult);
    }

    @Operation(summary = "新增角色")
    @PostMapping("/add")
    public R add(@RequestBody SysRole sysRole) {
        boolean result = sysRoleService.save(sysRole);
        return result ? R.success() : R.error();
    }

    @Operation(summary = "更新角色")
    @PutMapping("/update")
    public R update(@RequestBody SysRole sysRole) {
        boolean result = sysRoleService.updateById(sysRole);
        return result ? R.success() : R.error();
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/delete")
    public R delete(@RequestParam Long id) {
        if(id == 1L){
            return R.error("超级管理员角色不能删除");
        }
        boolean result = sysRoleService.removeById(id);
        return result ? R.success() : R.error();
    }
}
